
#ifndef COMMON_H
#define COMMON_H
#include <netinet/in.h>
#include <stdio.h>
#define ID_RIFF 0x46464952
#define ID_WAVE 0x45564157
#define ID_FMT 0x20746d66
#define ID_DATA 0x61746164

struct wav_header {
    uint32_t riff_id;
    uint32_t riff_sz;
    uint32_t riff_fmt;
    uint32_t fmt_id;
    uint32_t fmt_sz;
    uint16_t audio_format;
    uint16_t num_channels;
    uint32_t sample_rate;
    uint32_t byte_rate;
    uint16_t block_align;
    uint16_t bits_per_sample;
    uint32_t data_id;
    uint32_t data_sz;
};

typedef struct {
    char device[100];
    int card_index;
    int device_index;
    int buffer_time;
    int period_time;
    int period_count;
    int period_size;
    int channels;
    int mic;
    int ref;
    int rate;
    int interval;
    char mode[10];
    char format[100];
    char channel_order[16];
    int external_offline;
    double feed_rate;
} recorder_t;

typedef struct {
    char mode[10];
    char device[100];
    int sampleRate;
    int channels;
    int interval;
} player_t;

// 定义结构体
typedef struct {
    char productID[100];
    char productKey[100];
    char productSecret[100];
    char profilePath[1024];
    int authTimeout;
    int noRegister;
} auth_t;

typedef struct {
    int rollback;
    char wakeupRes[200];
    char sspeRes[200];
    char env[1024];
    char mdsRes[200];
    char wakeupWordsConf[512];
    int needStampTime;
} wakeup_t;

typedef struct {
    int outputChans;
    int needStampTime;
} extendsp_t;

typedef struct {
    int vad_pausetime;
    char vad_res[200];
} asr_t;

typedef struct {
    int sdk_log_level;
    int app_log_level;
    int fifo_size_in_second; // 原始音频缓存长度, 以秒记
    int feed_end_wait_in_second; // 离线feed音频结束后等待时间, 以秒记
    char dump_pcm_path[1024]; // PCM dump路径
} debug_t;

typedef struct {
    auth_t auth;
    recorder_t recorder;
    player_t player;
    wakeup_t wakeup;
    extendsp_t extendsp;
    asr_t asr;
    debug_t debug;
    char offline_audio_path[1024]; // 离线测试原始音频路径
    char description[100];
    char original_data[2048]; // 配置的原始内容
    char device_id[100]; // 设备ID
} app_config_t;

int auth(app_config_t config);
app_config_t init(const char* filename);
void arrange_channels(char* src, char* dest, int len, int num_channels, int new_num_channels, int* new_order);
void run_audio_provider(app_config_t config);
void run_audio_player(app_config_t config);
void addWavHeader(unsigned channels, unsigned int frames, FILE* file);
unsigned char* getDeviceId();
void dumpRawAudio(char* buffer, int len);
void print_json(const char* title, const char* json_str);
void dump_stream_orig(char* buffer, int len);
void dump_stream_in(char* buffer, int len);
void dump_stream_bf(char* buffer, int len);
void dump_stream_voip(char* buffer, int len);
void close_pcm_dump_file();
void deinit();
#endif // COMMON_H
